{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "csv_data = pd.read_csv('D:\\data.csv', index_col=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "csv_data.replace(['female', 'male'], [0, 1], inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = []\n",
    "label = []\n",
    "import random\n",
    "for row in csv_data.itertuples():\n",
    "    sample = list(row)\n",
    "    # Gender one-hot encoding - step 2. female: (0, 1); male: (1, 0).\n",
    "    if sample[1] == 0:\n",
    "        sample.insert(2, int(1))\n",
    "    else:\n",
    "        sample.insert(2, int(0))\n",
    "    # Cholesterol Numerization. normal: 0-100; high: 100-159; too high: 160-189.\n",
    "    # https://cn.bing.com/search?q=normal+cholesterol+levels+for+men&lvl=1&FORM=PMETIS&filters=ans%3a%22cvns%22+level%3a%220%22+mcid%3a%226198d203ed8243a6a23a1c9b2df02f2a%22&idx=0\n",
    "    if sample[7] == 'normal':\n",
    "        sample[7] = random.uniform(0,100)\n",
    "    elif sample[7] == 'high':\n",
    "        sample[7] = random.uniform(100,159)\n",
    "    elif sample[7] == 'too high':\n",
    "        sample[7] = random.uniform(160,189)\n",
    "    \n",
    "    # Glucose Numerization. normal: 72-99; high: 99-130; too high: 130-200.\n",
    "    # https://www.medicinenet.com/normal_blood_sugar_levels_in_adults_with_diabetes/article.htm\n",
    "    if sample[8] == 'normal':\n",
    "        sample[8] = random.uniform(72,110)\n",
    "    elif sample[8] == 'high':\n",
    "        sample[8] = random.uniform(110,126)\n",
    "    elif sample[8] == 'too high':\n",
    "        sample[8] = random.uniform(126,200)\n",
    "    #print(list(sample)[:-1])\n",
    "    data.append(list(sample)[:-1])\n",
    "    label.append(sample[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "X = np.array(data)\n",
    "y = np.array(label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.75\n",
      "0.685\n",
      "0.69\n",
      "0.765\n",
      "0.715\n",
      "Ave acc: 0.721\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import KFold\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "num_fold = 5\n",
    "\n",
    "kf = KFold(n_splits=num_fold, shuffle=True, random_state=0)\n",
    "ave_acc = 0\n",
    "for train_index, test_index in kf.split(X):\n",
    "    X_train, X_test = X[train_index] , X[test_index]\n",
    "    y_train, y_test = y[train_index], y[test_index]\n",
    "    \n",
    "    clf = DecisionTreeClassifier(random_state = 100, criterion = 'gini', max_depth = 1,min_samples_split = 500, max_features = 10)\n",
    "  \n",
    "    clf.fit(X_train, y_train)\n",
    "    y_pred = clf.predict(X_test)    \n",
    "    acc = accuracy_score(y_pred, y_test)\n",
    "    print(acc)\n",
    "    ave_acc = acc + ave_acc\n",
    "\n",
    "print(\"Ave acc: \" + str(ave_acc/num_fold))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
